package com.example.study.commen.commenutils;

import com.example.study.manager_plus.mybatis_plus.entity.CameraInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class CommonUtils {

    //传入指定时间
    public void convertToTimestamp(String time) {
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(time);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            long timestamp = cal.getTimeInMillis();
            System.out.println("10位时间戳=" + timestamp);
            System.out.println("13位时间戳" + timestamp / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    //传入时间戳即可
    public String conversionTime(String timeStamp) {
        //yyyy-MM-dd HH:mm:ss 转换的时间格式  可以自定义
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //转换
        String time = sdf.format(new Date(Long.parseLong(timeStamp)));
        return time;
    }

    public static void parsingFiles(File file) throws IOException {


        List<String[]> data = new ArrayList<>();

        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0); // 读取第一个sheet

            // 遍历行
            for (Row row : sheet) {
                String[] rowData = new String[row.getLastCellNum()];
                for (int i = 0; i < row.getLastCellNum(); i++) {
                    Cell cell = row.getCell(i);
                    rowData[i] = getCellValue(cell);

                }
                data.add(rowData);

            }

            // 打印数据
            for (int i = 0; i < data.size(); i++) {
                String[] row = data.get(i);
                CameraInfo cameraInfo = new CameraInfo();
                cameraInfo.setId((long) i);
                cameraInfo.setCameraProductName(row[0]);
                cameraInfo.setCommissioner(row[1]);
                cameraInfo.setManufacturer(row[2]);
                cameraInfo.setExternalModel(row[3]);
                cameraInfo.setWifiBand(row[4]);
                cameraInfo.setWifiProtocol(row[5]);
                cameraInfo.setMacAddress(row[6]);
                cameraInfo.setDefaultSsid(row[7]);
                cameraInfo.setSn(row[8]);
                cameraInfo.setBarCode(row[9]);
                log.info("lufei", cameraInfo.toString());
            }


        } catch (IOException e) {
            e.printStackTrace();

        }

    }

    private static String getCellValue(Cell cell) {
        if (cell == null) return "";
        switch (cell.getCellType()) {

            case STRING:
                return cell.getStringCellValue();

            case NUMERIC:
                return String.valueOf((int) cell.getNumericCellValue());

            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());

            case FORMULA:
                return cell.getCellFormula();

            default:
                return "";

        }

    }

}
